import bean.param as param
import vehicle_analysis_py as measure_main

from datetime import datetime

import os

import url_util


def analysis(req:param.AnalysisReq, data: param.MeasureAnalysisData, NN_df, CAE_df, bridge_info_dict):
    print("analysis 车载动力分析开始,code:{}".format(req.bridgeCode))

    bridgeName = req.bridgeName
    bridgeCode = req.bridgeCode
    bridgeType=req.bridgeType

    # 建立文件存储路径
    bridge_info_dir = f'bridge_info/{bridgeCode}/'
    KDE_pic_dir = f'KDE_pic/{bridgeCode}/'
    # 不存在则创建
    create_dir_if_not_exists(bridge_info_dir)
    create_dir_if_not_exists(KDE_pic_dir)
    # 文件路径+名字前缀
    format_time=datetime.now().strftime('%Y%m%d%H%M%S')

    bridge_info_path = f'{bridge_info_dir}{bridgeCode}_{format_time}'
    KDE_pic_path = f'{KDE_pic_dir}{bridgeCode}_{format_time}'

    param_dict = {
                        'sample_type': data.sample_type,
                        '采样频率': int(data.sample_frequency),
                        '桥梁全长': float(req.preEvaluate.mainSpan),
                        '桥梁EPSG代号': int(data.epsg),
                    }
    # 最终结果
    res = measure_main.vehicle_analysis_module(param_dict, url_util.format_local_url_list(data.sample_urls), NN_df, CAE_df, bridgeType, \
                                                bridgeName, bridgeCode, \
                                                bridge_info_dict, bridge_info_path, \
                                                KDE_pic_path)
    print("analysis 车载动力分析结束,code:{}".format(req.bridgeCode))
    return res

def create_dir_if_not_exists(dir_path):
    """
    如果指定路径的文件夹不存在，则创建文件夹。
    :param dir_path: 文件夹路径
    :return: 无
    """
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
        print(f"文件夹 {dir_path} 已创建。")
    else:
        print(f"文件夹 {dir_path} 已存在。")
